package com.gimi.cloud.bbp.manage.service.mall.impl;

import com.gimi.cloud.bbp.common.enums.ResponseCodeEnum;
import com.gimi.cloud.bbp.common.exception.MsgException;
import com.gimi.cloud.bbp.common.utils.ReflectUtils;
import com.gimi.cloud.bbp.dao.dao.mall.MallAppColumnDao;
import com.gimi.cloud.bbp.dao.myMapper.core.query.QueryParam;
import com.gimi.cloud.bbp.dto.mall.MallAppColumnDTO;
import com.gimi.cloud.bbp.entity.mall.MallAppColumnEntity;
import com.gimi.cloud.bbp.manage.service.mall.MallAppColumnService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 首页栏目相关service实现类
 * @author wbj
 * @version 1.0
 * @Description
 * @create 2018-02-01
 **/
@Service
public class MallAppColumnServiceImpl implements MallAppColumnService {
    private static final Logger logger = LoggerFactory.getLogger(MallAppColumnServiceImpl.class);
    private MallAppColumnDao mallAppColumnDao;
    @Autowired
    public MallAppColumnServiceImpl(MallAppColumnDao mallAppColumnDao) {
        this.mallAppColumnDao = mallAppColumnDao;
    }

    @Override
    public List<MallAppColumnDTO> queryList(MallAppColumnDTO dto, boolean isPaging) throws Exception {
        if(isPaging){
            PageHelper.startPage(dto.getPageNums(),dto.getPageSizes());
        }
        String sql = "SELECT col.* FROM mall_app_column col WHERE col.is_delete = 0";
        QueryParam queryParam = new QueryParam();
        queryParam.setSql(sql);
        queryParam.setParamObject(dto);
        return mallAppColumnDao.selectListDTOBySql(queryParam);
    }

    @Override
    public MallAppColumnDTO load(MallAppColumnDTO dto, Object id) throws Exception {
        MallAppColumnEntity mallAppColumn = mallAppColumnDao.queryBy("acl_id",id);
        if(null == mallAppColumn){
            return null;
        }
        MallAppColumnDTO columnDTO = new MallAppColumnDTO();
        ReflectUtils.copyPropertiesIgnoreNull(mallAppColumn,columnDTO);
        return columnDTO;
    }

    @Override
    public MallAppColumnDTO saveOrUpdate(MallAppColumnDTO dto) throws Exception {
        MallAppColumnEntity mallColumn = null;
        if (null != dto.getAclId()) {
            mallColumn = mallAppColumnDao.queryBy("acl_id", dto.getAclId());
            if (null == mallColumn) {
                throw new MsgException(ResponseCodeEnum.NO_DATA,"未获取Id为" + dto.getAclId() + "的栏目信息，更新失败");
            }
        } else {
            mallColumn = new MallAppColumnEntity();
        }
        ReflectUtils.copyPropertiesIgnoreNull(dto, mallColumn);
        if (null != dto.getAclId()) {
            mallAppColumnDao.baseUpdateByPrimaryKey(mallColumn);
        } else {
            mallAppColumnDao.baseInsert(mallColumn);
        }
        ReflectUtils.copyPropertiesIgnoreNull(mallColumn, dto);
        return dto;
    }

    @Override
    public int delete(MallAppColumnDTO dto, Object id, boolean realDel) throws Exception {
        MallAppColumnEntity mallAppColumn = mallAppColumnDao.queryBy("acl_id", id);
        if (null == mallAppColumn) {
            throw new MsgException(ResponseCodeEnum.NO_DATA,"Id" + id + "的栏目信息，删除失败");
        }
        //非物理删除
        if(!realDel){
            ReflectUtils.copyPropertiesIgnoreNull(dto,mallAppColumn);
            mallAppColumn.setIsDelete(1);
            return mallAppColumnDao.baseUpdateByPrimaryKey(mallAppColumn);
        }
        return mallAppColumnDao.deleteByPrimaryKey(id);
    }
}
